
#include "tutorfs.h"

#define TUTORFS_DEFAULT_MODE	0755
#define TUTORFS_MAGIC 0x858458f7

static const struct super_operations tutorfs_ops;
static const struct inode_operations tutorfs_dir_inode_operations;
struct socket *tutorfs_sock;

static struct backing_dev_info tutorfs_backing_dev_info = {
	.name		= "tutorfs",
	.ra_pages	= 0,	/* No readahead */
	.capabilities	= BDI_CAP_NO_ACCT_AND_WRITEBACK |
			  BDI_CAP_MAP_DIRECT | BDI_CAP_MAP_COPY |
			  BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP,
};

struct inode *tutorfs_get_inode(struct super_block *sb,
				const struct inode *dir, int mode, dev_t dev)
{
	struct inode * inode = new_inode(sb);
	printk("tutorfs_get_inode called\n");

	if (inode) {
		inode->i_ino = get_next_ino();
		inode_init_owner(inode, dir, mode);
		inode->i_mapping->a_ops = &tutorfs_aops;
		inode->i_mapping->backing_dev_info = &tutorfs_backing_dev_info;
		mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER);
		mapping_set_unevictable(inode->i_mapping);
		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
		switch (mode & S_IFMT) {
		default:
			init_special_inode(inode, mode, dev);
			break;
		case S_IFREG:
			inode->i_op = &tutorfs_file_inode_operations;
			inode->i_fop = &tutorfs_file_operations;
			break;
		case S_IFDIR:
			inode->i_op = &tutorfs_dir_inode_operations;
			inode->i_fop = &simple_dir_operations;

			/* directory inodes start off with i_nlink == 2 (for "." entry) */
			inc_nlink(inode);
			break;
		case S_IFLNK:
			inode->i_op = &page_symlink_inode_operations;
			break;
		}
	}
	return inode;
}

/*
 * File creation. Allocate an inode, and we're done..
 */
/* SMP-safe */
static int
tutorfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
{
	struct inode * inode = tutorfs_get_inode(dir->i_sb, dir, mode, dev);
	int error = -ENOSPC;
	printk("tutorfs_mknod called\n");
	if (inode) {
		d_instantiate(dentry, inode);
		dget(dentry);	/* Extra count - pin the dentry in core */
		error = 0;
		dir->i_mtime = dir->i_ctime = CURRENT_TIME;
	}
	return error;
}

static int tutorfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
{
	int retval = tutorfs_mknod(dir, dentry, mode | S_IFDIR, 0);
	struct req_mkdir reqmkdir;
	struct res_ok resok;
	char pathname[256];

	printk("tutorfs_mkdir called\n");
	if (!retval){
		inc_nlink(dir);

		// mkdir call
		get_dentry_path(pathname, 256, dentry);
//		printk("dentry_path : %s\n", pathname);

		memset(&reqmkdir, 0, sizeof(reqmkdir));

		strcpy(reqmkdir.dirname, pathname);
		if (request_protocol((byte)TYPE_REQ_MKDIR, (void *)&reqmkdir, (int)sizeof(reqmkdir), (void *)&resok, (int)sizeof(resok), tutorfs_sock) < 0)
			return -1;

		if (resok.isok == 0)
			return -1;
	}

	return retval;
}

static int tutorfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
{
	printk("tutorfs_create called\n");
	return tutorfs_mknod(dir, dentry, mode | S_IFREG, 0);
}

static int tutorfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
{
	struct inode *inode;
	int error = -ENOSPC;
	printk("tutorfs_symlink called\n");
	inode = tutorfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0);
	if (inode) {
		int l = strlen(symname)+1;
		error = page_symlink(inode, symname, l);
		if (!error) {
			d_instantiate(dentry, inode);
			dget(dentry);
			dir->i_mtime = dir->i_ctime = CURRENT_TIME;
		} else
			iput(inode);
	}
	return error;
}

struct dentry *tutorfs_lookup(struct inode *dir,
                          struct dentry *dentry, struct nameidata *nd) {
	printk("tutorfs_lookup called\n");
	return simple_lookup(dir, dentry, nd);
}

int tutorfs_link(struct dentry *old_dentry,
          struct inode *dir,
          struct dentry *dentry) {
	printk("tutorfs_link called\n");

	return simple_link(old_dentry, dir, dentry);
}

int tutorfs_unlink(struct inode *dir,
            struct dentry *dentry) {
	char pathname[256];
	struct req_rmdir reqrmdir;
	struct res_ok resok;

	printk("tutorfs_unlink called\n");

	get_dentry_path(pathname, 256, dentry);
	memset(&reqrmdir, 0, sizeof(reqrmdir));

	strcpy(reqrmdir.dirname, pathname);
	if (request_protocol((byte)TYPE_REQ_RMDIR, (void *)&reqrmdir, (int)sizeof(reqrmdir), (void *)&resok, (int)sizeof(resok), tutorfs_sock) < 0)
		return -1;

	if (resok.isok == 0)
		return -1;

	return simple_unlink(dir, dentry);
}

int tutorfs_rmdir(struct inode *dir,
           struct dentry *dentry) {
	struct req_rmdir reqrmdir;
	struct res_ok resok;
	char pathname[256];

	printk("tutorfs_rmdir called\n");

	// rmdir call
	memset(&reqrmdir, 0, sizeof(reqrmdir));
	get_dentry_path(pathname, 256, dentry);

	memset(&reqrmdir, 0, sizeof(reqrmdir));

	strcpy(reqrmdir.dirname, pathname);
	if (request_protocol((byte)TYPE_REQ_RMDIR, (void *)&reqrmdir, (int)sizeof(reqrmdir), (void *)&resok, (int)sizeof(resok), tutorfs_sock) < 0)
		return -1;

	if (resok.isok == 0)
		return -1;

	return simple_rmdir(dir, dentry);
}

int tutorfs_rename(struct inode *old_dir,
            struct dentry *old_dentry,
            struct inode *new_dir,
            struct dentry *new_dentry) {
	printk("tutorfs_rename called\n");

	return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
}

static const struct inode_operations tutorfs_dir_inode_operations = {
	.create		= tutorfs_create,
	.lookup		= tutorfs_lookup,
	.link		= tutorfs_link,
	.unlink		= tutorfs_unlink,
	.symlink	= tutorfs_symlink,
	.mkdir		= tutorfs_mkdir,
	.rmdir		= tutorfs_rmdir,
	.mknod		= tutorfs_mknod,
	.rename		= tutorfs_rename,
};

static const struct super_operations tutorfs_ops = {
	.statfs		= simple_statfs,
	.drop_inode	= generic_delete_inode,
	.show_options	= generic_show_options,
};

struct tutorfs_mount_opts {
	umode_t mode;
};

enum {
	Opt_mode,
	Opt_err
};

static const match_table_t tokens = {
	{Opt_mode, "mode=%o"},
	{Opt_err, NULL}
};

struct tutorfs_fs_info {
	struct tutorfs_mount_opts mount_opts;
};

static int tutorfs_parse_options(char *data, struct tutorfs_mount_opts *opts)
{
	substring_t args[MAX_OPT_ARGS];
	int option;
	int token;
	char *p;

	opts->mode = TUTORFS_DEFAULT_MODE;

	while ((p = strsep(&data, ",")) != NULL) {
		if (!*p)
			continue;

		token = match_token(p, tokens, args);
		switch (token) {
		case Opt_mode:
			if (match_octal(&args[0], &option))
				return -EINVAL;
			opts->mode = option & S_IALLUGO;
			break;
		/*
		 * We might like to report bad mount options here;
		 * but traditionally tutorfs has ignored all mount options,
		 * and as it is used as a !CONFIG_SHMEM simple substitute
		 * for tmpfs, better continue to ignore other mount options.
		 */
		}
	}

	return 0;
}

int tutorfs_fill_super(struct super_block *sb, void *data, int silent)
{
	struct tutorfs_fs_info *fsi;
	struct inode *inode = NULL;
	struct dentry *root;
	int err;

	save_mount_options(sb, data);

	fsi = kzalloc(sizeof(struct tutorfs_fs_info), GFP_KERNEL);
	sb->s_fs_info = fsi;
	if (!fsi) {
		err = -ENOMEM;
		goto fail;
	}

	err = tutorfs_parse_options(data, &fsi->mount_opts);
	if (err)
		goto fail;

	sb->s_maxbytes		= MAX_LFS_FILESIZE;
	sb->s_blocksize		= PAGE_CACHE_SIZE;
	sb->s_blocksize_bits	= PAGE_CACHE_SHIFT;
	sb->s_magic		= TUTORFS_MAGIC;
	sb->s_op		= &tutorfs_ops;
	sb->s_time_gran		= 1;

	inode = tutorfs_get_inode(sb, NULL, S_IFDIR | fsi->mount_opts.mode, 0);
	if (!inode) {
		err = -ENOMEM;
		goto fail;
	}

	root = d_alloc_root(inode);
	sb->s_root = root;
	if (!root) {
		err = -ENOMEM;
		goto fail;
	}

	return 0;
fail:
	kfree(fsi);
	sb->s_fs_info = NULL;
	iput(inode);
	return err;
}

struct dentry *tutorfs_mount(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *data)
{
	return mount_nodev(fs_type, flags, data, tutorfs_fill_super);
}

static void tutorfs_kill_sb(struct super_block *sb)
{
	kfree(sb->s_fs_info);
	kill_litter_super(sb);
}

static struct file_system_type tutorfs_fs_type = {
	.name		= "tutorfs",
	.mount		= tutorfs_mount,
	.kill_sb	= tutorfs_kill_sb,
};

static int __init init_tutorfs_fs(void)
{
	tutorfs_sock = set_up_client_socket(0x0ad33702, 9090);
	if (tutorfs_sock == 0)
		return -1;
	return register_filesystem(&tutorfs_fs_type);
}

static void __exit exit_tutorfs_fs(void)
{
	unregister_filesystem(&tutorfs_fs_type);
	if (tutorfs_sock) {
		release_socket(tutorfs_sock);
	}
}

module_init(init_tutorfs_fs)
module_exit(exit_tutorfs_fs)

MODULE_LICENSE("GPL");
